#include "MonitorCard.h"
#include "ui_MonitorCard.h"
#include "Tools/MqttClient.h"

#include <QPropertyAnimation>
#include <QGraphicsDropShadowEffect>
#include <QJsonObject>

MonitorCard::MonitorCard(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MonitorCard)
{
    ui->setupUi(this);

    // 设置透明度动画
    setWindowOpacity(0); // 初始透明度为 0
    setMinimumSize(0, 0); // 初始最小尺寸为 (0, 0)
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    for (const auto& child : this->findChildren<QWidget*>()) {
        child->setMinimumSize(0, 0);
        child->setVisible(true);
    }

    // 设置卡片显示时的动画效果
    setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_DeleteOnClose);

    setStyleSheet(R"(
        MonitorCard {
            background-color: white;
            border-radius: 4px;
            padding: 12px;
        }
    )");

    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(10);
    shadow->setColor(QColor(0, 0, 0, 100));
    shadow->setOffset(2, 2);
    setGraphicsEffect(shadow);

    setContentsMargins(12, 12, 12, 12);

    setMinimumSize(200, 150);

    connect(&MqttClient::getInstance(), &MqttClient::data_recived, this, [=](QJsonObject newData){
        if(newData["device_id"] == "TemperatureSensor")
        {
            this->ui->label_temperature->setText(newData["data"].toString());
        }

    });

    connect(&MqttClient::getInstance(), &MqttClient::data_recived, this, [=](QJsonObject newData){
        if(newData["device_id"] == "HumiditySensor")
        {
            this->ui->label_humidity->setText(newData["data"].toString());
        }

    });

}

MonitorCard::~MonitorCard()
{
    delete ui;
}
